from collections import defaultdict

from django.db import models

from apps.common.model import BaseModel
from django_pydantic_field import SchemaField


class Category(BaseModel):
    name = models.CharField(max_length=255, unique=True, verbose_name="类目名称")
    title = models.CharField(max_length=255, verbose_name="标题")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    cover_image = models.ImageField(upload_to='category/', blank=True, null=True, verbose_name="封面图片")
    parent = models.ForeignKey(
        'self',
        on_delete=models.SET_NULL,
        related_name='children',
        blank=True,
        null=True,
        verbose_name="父类目"
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "类目"
        verbose_name_plural = "类目"
        ordering = ['name']


# ------------------ product ------------------

class Product(BaseModel):
    """
    产品模型，包含产品的基本信息、技术数据、尺寸标注、下载信息和视频链接。
    """

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '产品'
        verbose_name_plural = '产品'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=["name"]),
        ]

    category = models.ForeignKey(
        Category,
        on_delete=models.SET_NULL,
        null=True,
        related_name='products',
        verbose_name='所属类目'
    )
    name = models.CharField(max_length=255, verbose_name='产品名称')
    cover_image = models.ImageField(upload_to='products/images/', blank=True, null=True, verbose_name="封面图片")
    brief_description = models.TextField(blank=True, null=True, verbose_name='产品简介')
    detailed_description = models.TextField(blank=True, null=True, verbose_name='产品详情描述')
    product_info = SchemaField(list[dict[str, list]], default=list, verbose_name="产品信息")
    technical_data = SchemaField(list[dict[str, list]], default=list, verbose_name="技术数据")
    video_links = SchemaField(list[str], default=list, verbose_name='视频链接列表')

    @property
    def media(self):
        product_files = self.downloads.all()
        medias = defaultdict(list)
        for product_file in product_files:
            medias[product_file.download_type].append(product_file)
        return medias


class ProductFile(BaseModel):
    """
    产品文件模型，表示与产品关联的具体文件。
    """

    DOWNLOAD_TYPE_CHOICES = [
        ('catalogue', '目录'),
        ('manual', '操作手册'),
        ('folder', '文件夹'),
        ('datasheet', '数据表'),
        ('dimension', '尺寸标注'),
        ('other', '其他'),
    ]

    def __str__(self):
        return self.file.name

    class Meta:
        verbose_name = '产品文件(图片/下载)'
        verbose_name_plural = '产品文件(图片/下载)'
        ordering = ['product_id']
        indexes = [
            models.Index(fields=["download_type"]),
            models.Index(fields=["name"]),
        ]

    file = models.FileField(upload_to='products/download_files/', verbose_name='文件')
    name = models.CharField(max_length=255, blank=True, null=True, verbose_name='文件名')
    download_type = models.CharField(
        max_length=50,
        choices=DOWNLOAD_TYPE_CHOICES,
        verbose_name='文件类型(目录/手册/文件夹/数据表/尺寸标注/其他)',
    )
    product = models.ForeignKey(
        Product,
        on_delete=models.SET_NULL,
        null=True,
        related_name='downloads',
        verbose_name='所属产品'
    )
